package com.clkj.zhonghui.service.stored;

import com.clkj.zhonghui.dao.stored.ActivityLotteryMapper;
import com.clkj.zhonghui.pojo.common.DateFormatHelper;
import com.clkj.zhonghui.pojo.common.OSSUtil;
import com.clkj.zhonghui.pojo.stored.ActivitySharingRecord;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author：周佳润
 * @Date：2024/7/18 上午9:17
 */
@Service
public class ActivityLotteryServiceImpl implements ActivityLotteryService{
    @Autowired
    private ActivityLotteryMapper activityLotteryMapper;

    @Override
    public void addActivityLottery(ActivitySharingRecord activitySharingRecord, HttpServletRequest request,String flag) {
        MultipartFile file = ((MultipartRequest) request).getFile("file");
        String fileName = "";
        if (file != null) {
            fileName  = OSSUtil.uploadingImg(file,"zhonghui/share");
        }
        if(flag.equals("add")){
            activitySharingRecord.setRecordPic(fileName);
            activitySharingRecord.setRecordFlag(0);
            activitySharingRecord.setRecordChou(0);
            activitySharingRecord.setRecordTime(DateFormatHelper.currentTimes());
            activityLotteryMapper.insertSelective(activitySharingRecord);
        }else{
            activityLotteryMapper.updateByPrimaryKeySelective(activitySharingRecord);
        }
    }

    @Override
    public PageInfo getActivityLotteryList(Integer shopId, Integer recordFlag, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Example example = new Example(ActivitySharingRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (shopId != 0){
            criteria.andEqualTo("shopId",shopId);
        }
        if (recordFlag != -1){
            criteria.andEqualTo("recordFlag",recordFlag);
        }
        example.orderBy("recordId").desc();
        List<ActivitySharingRecord> activitySharingRecordList = activityLotteryMapper.selectByExample(example);
        return new PageInfo(activitySharingRecordList);
    }

    @Override
    public void authActivityLottery(ActivitySharingRecord activitySharingRecord) {
        if (activitySharingRecord.getRecordId() != null){
            activityLotteryMapper.updateByPrimaryKeySelective(activitySharingRecord);
        }
    }

    @Override
    public List<ActivitySharingRecord> getActivityUserList(Integer userId) {
        Example example = new Example(ActivitySharingRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (userId != null){
            criteria.andEqualTo("userId",userId);
        }
        example.orderBy("recordId").desc();
        List<ActivitySharingRecord> activitySharingRecordList = activityLotteryMapper.selectByExample(example);
        return activitySharingRecordList;
    }

}
